#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;
//
// Created by lenovo on 24-12-25.
//
namespace zzy
{
    class string
    {
    public:
        //string(const char* str = '\0')
        //string(const char* str = nullptr)
        //string(const char* str = "\0")
        explicit string(const char* str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_capacity+1];
            memcpy(_str, str,_size+1);
        }

        string(const string&s)
        {
            _str = new char[s._capacity+1];
            memcpy(_str, s._str,s._size+1);
            _size = s.size();
            _capacity = s._capacity;
            _str = new char[_capacity+1];
        }

        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }

        const char* c_str() const
        {
            return _str;
        }

        size_t size() const
        {
            return _size;
        }

        char& operator[](size_t pos)
        {
            assert(pos < _size);
            return _str[pos];
        }
        const char& operator[](size_t pos) const
        {
            assert(pos < _size);
            return _str[pos];
        }

        typedef char* iterator;
        typedef const char* const_iterator;

        iterator begin()
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }
        const_iterator begin() const
        {
            return _str;
        }
        const_iterator end() const
        {
            return _str + _size;
        }

        void reserve(size_t n)
        {
            if(n > _capacity)
            {
                char* tmp = new char[n+1];
                memcpy(tmp,_str,_size+1);
                delete[] _str;
                _str = tmp;
                _capacity = n;
            }
        }

        void push_back(char ch)
        {
            if(_size == _capacity)
            {
                reserve(_capacity == 0 ? 4: _capacity*2);
            }
            _str[_size] = ch;
            ++_size;
            _str[_size] = '\0';
        }

        void append(const char* str)
        {
            size_t len = strlen(str);
            if(_size + len > _capacity)
            {
                reserve(_size+len);
            }
            memcpy(_str+_size, str, len+1);
            _size += len;
        }

        string& operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }
        string operator+=(const char* str)
        {
            append(str);
            return *this;
        }

        void insert(size_t pos, size_t n, char ch)
        {
            assert(pos<=_size);
            if(_size + n > _capacity)
            {
                reserve(_size+n);
            }
            size_t end = _size;
            while (end>=pos && end!=npos)
            {
                _str[end+n] = _str[end];
                --end;
            }
            for (size_t i=0; i<n; i++)
            {
                _str[pos+i] = ch;
            }
            _size += n;
        }

        void resize(size_t n, char ch = '\0')
        {
            if (n<_size)
            {
                _size = n;
                _str[_size] = '\0';
            }
            else
            {
                reserve(n);
                for (size_t i = 0; i<n; i++)
                {
                    _str[i] = ch;
                }
                _size = n;
                _str[_size] = '\0';
            }
        }

        void erase(size_t pos, size_t len = npos)
        {
            assert(pos <= _size);
            if(len==npos || pos + len >= _size)
            {
                _str[pos] = '\0';//将字符串从 pos 处截断。
                _size = pos;//字符串的有效长度现在是从索引 0 到 pos - 1，索引 pos 以及之后的内容被认为是无效的。
                _str[_size] = '\0';//再次确保字符串有结束符
            }
            else
            {
                size_t end = pos + len;
                while (end<=_size)
                {
                    _str[pos++] = _str[end++];
                }
                _size -= len;
            }
        }

        size_t find(char ch, size_t pos = 0)
        {
            for (size_t i=pos; i<_size; i++)
            {
                if(_str[i] == ch)
                {
                    return i;
                }
            }
            return npos;
        }
        size_t find(const char* str, size_t pos = 0)
        {
            assert(pos<_size);
            const char* ptr = strstr(_str + pos, str);

            if(ptr)
            {
                return ptr-_str;//字符串索引是通过整数值表示的，而 ptr - _str 正好返回了这个整数值
            }
            else
            {
                return npos;
            }
        }

        string substr(size_t pos = 0, size_t len = npos)
        {
            assert(pos < _size);

            size_t n = len;
            if(len == npos || pos + len > _size)
            {
                n = _size - pos;
            }
            string tmp;
            tmp.reserve(n);
            for (size_t i=pos; i<n; i++)
            {
                tmp += _str[i];
            }
            return tmp;
        }
        void clear()
        {
            _str[0] = '\0';
            _size = 0;
        }

        bool operator<(const string& s) const
        {
            size_t i1 = 0;
            size_t i2 = 0;
            while (i1 < _size && i2 < _size)
            {
                if(_str[i1] < s._str[i2])
                {
                    return true;
                }
                else if(_str[i1] > s._str[i2])
                {
                    return false;
                }
                else
                {
                    ++i1;
                    ++i2;
                }
            }
            return i1 == _size && i2 != s._size;
        }
        bool operator==(const string& s) const
        {
            return _size == s._size
                && memcmp(_str, s._str, _size) == 0;
        }
        bool operator<=(const string& s) const
        {
            return *this < s || *this == s;
        }
        bool operator>(const string& s) const
        {
            return !(*this <= s);
        }
        bool operator>=(const string& s) const
        {
            return !(*this < s);
        }
        bool operator!=(const string& s) const
        {
            return !(*this == s);
        }

        // string& operator=(const string& s)
        // {
        //     if(this != &s)
        //     {
        //         char* tmp = new char[s._capacity + 1];
        //         memcpy(tmp, s._str, s._size+1);
        //         delete[] _str;
        //         _str = tmp;
        //
        //         _size = s._size;
        //         _capacity = s._capacity;
        //     }
        //     return *this;
        // }
        string& operator=(string tmp)
        {
            swap(tmp);
            return *this;
        }
        void swap(string& s)
        {
            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity, s._capacity);
        }

    private:
        size_t _size;
        size_t _capacity;
        char* _str;

        static size_t npos;
    };
    size_t string::npos = -1;

    ostream& operator<<(ostream& out, const string& s)
    {
        for (auto ch : s)
        {
            out << ch;
        }
        return out;
    }
    istream& operator>>(istream& in, string& s)
    {
        s.clear();

        char ch = in.get();
        while (ch == ' ' || ch == '\n')
        {
            ch = in.get();
        }

        char buff[128];
        int i = 0;
        while (ch != ' ' && ch != '\n')
        {
            buff[i++] = ch;
            if (i == 127)
            {
                buff[i] = '\0';//buff装满了，把buff的存入s，将buff清空
                s += buff;
                i = 0;
            }
            ch = in.get();
        }

        if (i != 0)
        {
            buff[i] = '\0';
            s += buff;
        }

        return in;
    }
}

void test_string1()
{
    zzy::string s1("hello world!");
    cout << s1.c_str() << endl;

    // zzy::string s2;
    // cout << s2.c_str() << endl;
    //
    // const zzy::string s3("hello world!");

    zzy::string::iterator it = s1.begin();
    while(it != s1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    for (auto ch : s1)
    {
        cout << ch << " ";
    }
    cout << endl;
}

void test_string2()
{
    zzy::string s1("hello world");
    cout << s1.c_str() << endl;
    s1.push_back(' ');
    s1.append("balabala");
    cout << s1.c_str() << endl;
    s1 += '\0';
    s1 += "!!!!";
    cout << s1.c_str() << endl;
    cout << s1 << endl;
}

int main()
{
    // test_string1();
    test_string2();
    return 0;
}